<script type="text/javascript">
	function HidePart(item) { 
		document.getElementById(item).style.display = "none";
		}
	function ShowPart(item) { 
		document.getElementById(item).style.display = "inline-block"; 
	}
	
	//$(document).ready(function() {
	//	$(".tasklist-container").each(function() {
	//		$(this).mouseover(function() {
	//			$(this).children("tasklistControl").show();
	//		});
	//		$(this).mouseout(function() {
	//			$(this).children("tasklistControl").hide();
	//		});
	//	});
	// });
</script>

<?php
    $jsShowTaskList = "
        function ShowTaskList(tasklistid){
            $(\".tasklist-container\").not(\"#tasklist-container-\"+tasklistid).hide();
            $(\"#tasklist-container-\"+tasklistid).show();
            return false;
        }
    ";
    
    $jsDelEditTaskRow = "
        $(\".taskRow\").mouseover(function(){
            $(this).find(\".taskControl\").show();
        });
        $(\".taskRow\").mouseout(function(){
            $(this).find(\".taskControl\").hide();
        });
        $(\".tasklist-container\").mouseover(function(){
            $(this).find(\".tasklistControl\").show();
        });
        $(\".tasklist-container\").mouseout(function(){
            $(this).find(\".tasklistControl\").hide();
        });
    ";
    
    $jsHideCompleteTask = "
        $(\".taskcomplete\").hide();
    ";
    
    $jsFilterUser = "
        $(\"#filterUser\").change(function()
        {
            document.location.href = $(this).val();
        });
    ";
    Yii::app()->clientScript->registerScript('ShowTaskList',$jsShowTaskList,CClientScript::POS_HEAD);
    Yii::app()->clientScript->registerScript('DelEditTaskRow',$jsDelEditTaskRow,CClientScript::POS_READY);
    Yii::app()->clientScript->registerScript('HideCompleteTask',$jsHideCompleteTask,CClientScript::POS_READY);
    Yii::app()->clientScript->registerScript('FilterUser',$jsFilterUser,CClientScript::POS_READY);
?>

<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/task.css" />
<link rel='stylesheet' type='text/css' href="<?php echo Yii::app()->theme->baseUrl; ?>/css/jquery-ui-1.8.14.custom.css" />
<script type='text/javascript' src="<?php echo Yii::app()->theme->baseUrl; ?>/fullCalendar/jquery/jquery-ui-1.8.11.custom.min.js"></script>
<script type='text/javascript' src="<?php echo Yii::app()->theme->baseUrl; ?>/js/jquery.ui.datepicker.js"></script>

<script type="text/javascript">
	$(document).ready(function(){
		
		$('#cancel').click(function(){
			$.fancybox.close();
			return false;
		});
		$('.addTaskTime').datepicker({
				dateFormat: 'yy-mm-dd'
		});
		$('.editTaskTime').datepicker();
		//Close Fancybox
		$('.popupCancel').click(function(){
				$.fancybox.close();
				return false;
			});
		
		$('input:checkbox').click(function(){
		
			//if($(this).is(':checked')){
			
			var value = $(this).val();
			var listPeople = '';
			var count = 0;
			$('input:checkbox:checked').each(function() {
				
				listPeople = listPeople + $(this).val();
				listPeople = listPeople +',';
				count++;
			})
			$("#listPeople").val(''+count+ 'persons ');
			listPeople = '';
			})
			//Close button
			$('#clear').click(function(){
					$("#addTask").fancybox();
					$('input:checkbox:checked ').each(function() {
						$('input:checkbox').attr('checked',false);
						count = 0;
						$("#listPeople").val(''+count+ 'persons ');
						
					})
				})
				//All None
				$('#all').click(function(){
					
						$('input:checkbox').attr('checked',true);
						count = 0;
						$("#listPeople").val(''+count+ 'persons ');
				
				})
				$('#none').click(function(){
					
						$('input:checkbox').attr('checked',false);
						count = 0;
						$("#listPeople").val(''+count+ 'persons ');
				})
		})
</script>
<div class="clear"></div>

			<div id="than">	
           	 <img src="<?php echo Html::imageUrl("muiten.png")?>" width="27" height="26"; style="float:left">
            	<div id="textxanhdam"> Tasks </div>	
			</div>
	
             <div class="padding15"></div>            	
             <!-- Begin leftSidebar -->   
			<div id="leftSidebar">
				<div id="leftWrapper">
					<h2>New task lists</h2>           		
            		<div class="padding15"></div>
            		<div class="padding5"></div>
                            <!-- List all user in select box -->
                            <select id="filterUser" "style="width:290px margin-left:10px;">
                                    <?php
                                        $pid = Yii::app()->request->getParam("pid");
                                        echo "<option value='{$this->createUrl('/tasks/index',array('pid'=>$pid))}'>Filter by user</option>";
                                        echo "<option value='{$this->createUrl('/tasks/index',array('pid'=>$pid))}'>All users</option>";
                                        foreach($users as $user){
                                            $user = User::model()->findByPk($user->project_user_user_id);
                                            $link = $this->createUrl('/tasks/index',array('pid'=>$pid,'uid'=>$user->user_id));
                                            echo "<option value='{$link}'>{$user->user_name}</option>";
                                        }
                                    ?>
                            </select>
            		<div class="padding15"></div>
                        <a href="#" onclick="javascript:$('.tasklist-container').show()"><h3>Active task lists</h3></a>
            		<div class="padding10"></div>
            		<div id="taskList">
            			<ul class="tasks">
                                        <?php
                                            foreach ($tasklists as $tasklist){
                                                $editTask = $tasklist['model'];
                                        ?>
                                                <li><a href="#" onclick="ShowTaskList('<?php echo $editTask->task_list_id;?>')"><font><?php echo $editTask->task_list_name; ?></font><span class="functionNo"></span></a></li>
                                                <br>
                                        <?php
                                            }
                                        ?>
            			</ul>            		
					</div>
				</div>
			</div>
			<div id="rightSidebar">
                     <div id="rightHeader">
						<h2>Tasks</h2>
						<button type="button" id="rightHeaderBtn" href="#addTaskList">Add task list</button>
                     </div>
                            <div id="rightWrapper">	
								<ul>
                                    <!-- Loop through each Task List -->
                                    <?php
                                        foreach ($tasklists as $tasklist){
                                            $editTask = $tasklist['model'];
                                            $tl = $editTask->task_list_id;
                                            $listTasks = $tasklist['listTasks'];
                                    ?>
                                        <li class="tasklist-container" id="tasklist-container-<?php echo $tl;?>">
                                            <div class="taskListName" id="tasklist-<?php echo $tl;?>"><?php echo $editTask->task_list_name;?> &nbsp
                                                <div class="tasklistControl" style="display:none">
                                                	<?php 
                                                		echo Chtml::link("edit","#editTaskList",
                                                			array(
                                                				'class'=>'tlEdit',
                                                				'onClick'=>"
                                        							$('#editTaskList #taskListName').val('$editTask->task_list_name');
                                        							$('#editTaskList #taskListDes').val('$editTask->task_list_description');
                                        							$('#editTaskList #tl_id').val('$tl');
                                        						",
                                                			)
                                                		);
                                                		
                                                		echo " ";
                                                		echo Chtml::ajaxLink("delete", $this->createUrl("/tasks/delTaskList"), 
                                                			array(
                                                				'beforeSend' => "function() {
                                        							return confirm('Are you sure');	
                                        						}",
                                                				'type'=>'POST',
                                                				'data'=>array('tl_id'=>$tl),
                                                				'success'=>"function() {
																	window.location.reload();                                        						
                                        						}",
                                                			), 
                                                			array(
                                                				'class'=>'tlDelete',
                                                			)
                                                		);
                                                	?> 
                                                </div>
                                            </div>
                                            <ul>
                                                <!-- Loop each Task -->
                                                <?php
                                                    $uid = Yii::app()->request->getParam("uid",0);
                                                    foreach ($listTasks as $task){
                                                        if (($uid == 0)or($task->taskCreatedByUser->user_id == $uid))
                                                        if ( !$task->task_deleted_date){
                                                        	$tid = $task->task_id;
                                                ?>
                                                    <li id="task-<?php echo $task->task_id;?>" class="taskRow">
                                                            <div class="taskName"><a href="<?php echo $this->createUrl("/tasks/detail",array("pid"=>$pid,"tid"=>$task->task_id))?>"> <?php echo $task->task_name;?></a></div>
                                                            <div class="taskCreatedUser">created by <?php echo $task->taskCreatedByUser->user_name;?></div>
                                                            <div class="taskCreatedTime">at <?php echo $task->task_created_date;?></div>
                                                            <div class="taskControl" style="display:none">
                                                            	<?php 
                                                            		echo Chtml::ajaxLink('delete', array('tasks/del'), 
                                                            			array(
                                                            				'beforeSend' => "function() {
                                        										return confirm('Are you sure');	
                                        									}",
                                        									'data'=>array('tid'=>$tid),
                                                            				'success'=>"function(){
                                                        						window.location.reload();
                                                        					}",
                                                            				'error'=>"function(){
                                                        						alert('Error occurped');
                                                        					}",
                                                            			), 
                                                            			array(
                                                            				'class'=>"tDelete",
                                                            			)
                                                            		);
                                                            		echo "&nbsp";
                                                            		echo Chtml::link(
                                                            			'edit', 
                                                            			$this->createUrl('tasks/edit', array('pid'=>$pid, 'tid'=>$tid))
                                                            		);
                                                            	?>
                                                            </div>
                                                    </li>
                                                <?php
                                                        }
                                                        else {
                                                ?>
                                                        <li id="task-<?php echo $task->task_id;?>" class="taskRow taskcomplete">
                                                            <div class="taskName"><?php echo $task->task_name;?></div>
                                                            <div class="taskCreatedUser">created by <?php echo $task->taskCreatedByUser->user_name;?></div>
                                                            <div class="taskCreatedTime">at <?php echo $task->task_created_date;?></div>
                                                            <div class="taskControl" style="display:none">
                                                                <span id="delTask-<?php echo $task->task_id;?>" ><a href="#">delete</a></span> &nbsp 
                                                                <span id="editTask-<?php echo $task->task_id;?>"><a href="#editTask" id="editTaskLink">edit</a></span>
                                                            </div>
                                                        </li>
                                                <?php
                                                            
                                                        }
                                                    }
                                                ?>
                                                <!--End loop task-->
                                            </ul>
                                            <div class="taskButton">
                                            	<?php echo Chtml::button('Add task',array(
                                            				'id'=>"addTaskId",
                                            				'href'=>"#addTask",
                                            				'onClick'=>"js:$('#tl-id').val('$tl');"
                                            		  ));
                                            	?>
                                            </div>
                                        </li>
                                    <?php
                                        }
                                    ?>
                                    <!--End Task List -->
                                </ul>
                            </div>
                        <!-- End rightSidebar -->
                        </div>
             
    </div>
    
    <!--  New TaskList Area -->
    <div id="popup-addTaskList" style="display:none">
		<?php $this->renderPartial('addTaskList', array('model'=>$tasklistModel))?>
    </div>
    <!-- End add task list -->
    
    <!--  edit TaskList Area -->
    <div id="popup-editTaskList" style="display:none">
		<div id="editTaskList" class="popupWrapper" method="post" action="#">
		    	<div class="popupHeader">
		    		<h2>Edit task list</h2>	
		    	</div>
		    	
		    	<div class="popupContent">
			    	<h3>Task list name</h3>
			    	<form action="#">
				    	<input type="text" id="taskListName" name="taskListName2" size="80px">
				    	<div class="padding15"></div>
				    	<label style="margin-top: 10px">Do you have any notes for this list? Enter them here:</label>
				    	<textarea name="taskListDes2" id="taskListDes" rows="4" cols="65"></textarea>
				    	<div class="postButton">
				    	<?php 
				    		echo Chtml::hiddenField("tl_id",null,array(
				    			'id'=>'tl_id',
				    		));
				    		echo Chtml::ajaxSubmitButton("Save this task list", $this->createUrl('/tasks/editTaskList'),
				    			array(
				    				'type'=>'POST',
				    				'data'=>"js:{
				    					taskListName: $('#editTaskList #taskListName').val(),
				    					taskListDes: $('#editTaskList #taskListDes').val(),
				    					tl_id : $('#editTaskList #tl_id').val()
				    				}",
				    				'success'=>"function(){
				    					window.location.reload();
				    				}",
				    				'error'=> "function(){
				    					alert('Error corrupted ! Please try again'); 
				    				}",
				    			),
				    			array(
				    			)
				    		);
				    	?>
			    		or
			    			<a href="#" class="popupCancel" >Cancel</a>
			    		</div>
			    	</form>
		    	</div>
		    	
	    </div>
    </div>
    <!-- End edit task list -->
    
    <!-- Add Task area -->
    <div id="addTaskPopup" style="display: none;">
    	<div id="addTask" class="popupWrapper" action="" method="post">
    		<div class="popupHeader">
    			<h2>Add a task</h2>
    		</div>
    		<div class="popupContent">
    			<?php $this->renderPartial('add',array('model'=>$taskModel))?>
    		</div>
    	</div>
    </div>
    <!-- Multiple people -->
    <div id="multiPeopleDiv" style="display: none;">
    	<div id="multiPeople" class="megaWrapper" action="" method="post">
    		<div class="megaHeader">
    			<h2>Team</h2>
    			<a id="all" href="#">All</a>
    			<a id="none" href="#">None</a>
    		</div>
    		<div class="popupContent">
			    		<INPUT NAME="options" TYPE="CHECKBOX" VALUE="o1" id="1">
			 					 Option 1<BR>
						<INPUT NAME="options" TYPE="CHECKBOX" VALUE="o2" id="2">
			 					Option 2<BR>
						<INPUT NAME="options" TYPE="CHECKBOX" VALUE="o3">
						  		Option 3<BR>
						<INPUT NAME="options" TYPE="CHECKBOX" VALUE="o4">
						 		 Option 4<BR>
						<INPUT NAME="options" TYPE="CHECKBOX" VALUE="o5">
						 		 Option 5<BR>
						<INPUT NAME="options" TYPE="CHECKBOX" VALUE="o6">
								  Option 6<BR>
						    		
    		</div>
    		<div class="postButton">
    				
    					<button  type="submit" id="multiSave" href="#addTask" >Save</button> 
    					<button id = "clear" href="#addTask">Clear Data</button> 
    					<button id = "back" href="#addTask">Back</button> 
		    			
    				</div>
    	</div>
    </div>
    
    <!-- Edit task popup -->
    
   
    <?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'button#back', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false,  'hideOnOverlayClick' =>false ), ) ); ?>
    <?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'input#addTaskId', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false,  'hideOnOverlayClick' =>false ), ) ); ?>
    <?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'a.tlEdit', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false,  'hideOnOverlayClick' =>false ), ) ); ?>
    <?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'a.taskEdit', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false,  'hideOnOverlayClick' =>false ), ) ); ?>
    <?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'button#multiSave', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false,  'hideOnOverlayClick' =>false ), ) ); ?>
    <?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'a#multipeople', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false,  'hideOnOverlayClick' =>false ), ) ); ?>
    <?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'button#rightHeaderBtn', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false,  'hideOnOverlayClick' =>false ), ) ); ?>